// pages/my/my.js
Page({
   /**
     * 页面的初始数据
     */
    data: {
        couponCount: 0,
        login:1,
        captcha:"获取验证码",
        phone:"",
        verification:"",
        disabled:0,
        show:false,
        meth:"",
       minDate: new Date(2022, 8-1, 1).getTime(),
       maxDate: new Date(2022, 9-1, 31).getTime(),
       signTime:"",
       lgoinClassify:1,
       signList:"",
       signListShow:false,
       nickname:"",
       user_image:"",
       IMuser:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
      
      let islogin = wx.getStorageSync('islogin')
      let that = this
      if (islogin) {
        let token = wx.getStorageSync('token')
        wx.request({
          url: 'http://www.house.com/index.php/api/usermessage',
          header:{
              token:token
          },
          success:res=>{
            console.log(res)
            that.setData({
              login:0,
              nickname:res.data.data.user_name,
              user_image:res.data.data.user_image,
              IMuser:res.data.data.im_id
            })
          }
        })
        that.setData({
          login:0
        })
      }else{
        that.setData({
          login:1
        })
      }
    },
    onGotoLinUI() {
        wx.navigateToMiniProgram({
            appId:'Lin-UI AppID'
        })
    },
    getphone(e){
        this.setData({
          phone:e.detail.value
        })
    },
    getVerificationCode(e){
      this.setData({
        verification:e.detail.value
      })
    },
    getCaptcha(){
      let phone = this.data.phone
      let that = this
      if (!phone) {
        wx.showToast({
          title: '请输入手机号',
          icon:"error"
        })
        return false
      }
      let time=60
      let countdown = setInterval(function(){
          time--
          
          if (time>0) {
            that.setData({
              captcha:time+"秒后获取",
              disabled:1
            })
          }else{
            that.setData({
              captcha:"获取验证码",
              disabled:0
            })
            clearInterval(countdown)
          }
         
      },1000) 
     
      wx.request({
        url:"http://www.house.com/index.php/api/getcaptcha",
        data:{
          phone:phone
        },
        method:"POST",
        success:res=>{
          console.log(res)
        }
      })
    },
    userLogin(){
      let phone = this.data.phone
      let verification = this.data.verification
      if (!phone) {
        wx.showToast({
          title: '请输入手机号',
          icon:"error"
        })
        return false
      }
      if (!verification) {
        wx.showToast({
          title: '请输入验证码',
          icon:"error"
        })
        return false
      }
      wx.login({
        success:res=>{
          wx.request({
            url:"http://www.house.com/index.php/api/phoneLogin",
            data:{
              phone:phone,
              verification:verification,
              code:res.code
            },
            method:"POST",
            success:res=>{
              let token = res.data.data
              if (res.data.code==200) {
                wx.setStorageSync('islogin', true)
                wx.setStorageSync('token', token)
                wx.showToast({
                  title: '登录成功',
                  icon:"success"
                })
                this.setData({
                  login:0,
              
                })
                 this.UserList();
              }else{
                wx.showToast({
                  title: '登录失败',
                  icon:"error"
                })
              }
            }
          })
        }
      })
     
    },
    phoneView(){
        let that = this
        that.setData({
          lgoinClassify:0
        })
    },

    wxLogin(){
       wx.getUserProfile({
         desc: '授权登录',
         success:result=>{
          wx.login({
            success:res=>{
              wx.request({
                url: 'http://www.house.com/index.php/api/authorizationlogin',
                data:{
                  nickname:result.userInfo.nickName,
                  headportrait:result.userInfo.avatarUrl,
                  code:res.code
                },
                method:"POST",
                success:res=>{
                  let token = res.data.data.token
                  if (res.data.code==200) {
                    wx.setStorageSync('islogin', true)
                    wx.setStorageSync('token', token)
                    wx.showToast({
                      title: '登录成功',
                      icon:"success"
                    })
                    this.setData({
                      nickname:res.data.data.nickname,
                      user_image:res.data.data.user_image,
                      IMuser:res.data.data.im_id,
                      login:0,
                    })
                  }else{
                    wx.showToast({
                      title: '登录失败',
                      icon:"error"
                    })
                  }
                }
              })
            }
          })
          
        }
       })
    },
    onIntegralShop(){
      wx.navigateTo({
        url: "/pages/integral-shop/integral-shop"
    })
    },
    recentContacts(){
      let IMuser = this.data.IMuser
      wx.navigateTo({
        url: "/pages/contacts/contacts?IMuser="+IMuser
    })
    },
    userChat(){
      wx.request({
        url: 'http://www.house.com/index.php/chat/user_token',
        header:{"token":wx.getStorageSync('token')},
        method:"POST",
        success:res=>{
          if(res.data.code==200){
             wx.navigateTo({
                url: '/pages/chat/chat?id='+res.data.data,
             })
          }
        }
      })
       
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    friend(){
      wx.navigateTo({
        url: "/pages/friend/friend"
    })
    },
    /**
     * 用户签到页面
     */
    onSign(){
        this.setData({
          show:true
        })
        
    },
    feedBack(){
      wx.navigateTo({
        url: '/pages/feedback/feedback',
      })
    },
    idcard(e){
      wx.navigateTo({
        url: '/pages/idcard/idcard',
      })
    }

})